package com.wiloon.java8x.thread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/*
 * Created by roy on 5/12/17;
 * http://www.aichengxu.com/java/5671264.htm
 */
public class MyThreadFactoryTest1 implements ThreadFactory {

    private final AtomicInteger count = new AtomicInteger(0);

    // private final String namePrefix = "jmsgPool-" + count.incrementAndGet() + "-" + "PersistenceWrite" + "-thread-";

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(r);
        t.setName("jmsgPool-" + count.incrementAndGet() + "-" + "PersistenceWrite" + "-thread");
        System.out.println("Create new thread, thread name: " + t.getName());
        return t;
    }

    /**
     * (测试)
     *
     * @param args
     */

    public static void main(String[] args) {
        ExecutorService service = Executors.newFixedThreadPool(5, new MyThreadFactoryTest1());

        for (int i = 0; i <= 5; i++) {
            service.submit(new Runnable() {

                @Override
                public void run() {
                    System.out.println("Start execute...");
                }
            });
        }

    }
}